<?php
/**
 * Created by IntelliJ IDEA.
 * User: sunminqiang
 * Date: 17/3/8
 * Time: 上午10:28
 */
namespace app\admin\controller;
use app\admin\model\DownloadModel;
use think\Db;

class Download extends Base
{

    /**
     * [index 文章列表]
     * @author [田建龙] [864491238@qq.com]
     */
    public function index()
    {

        $key = input('key');
        $map = [];
        if ($key && $key !== "") {
            $map['name'] = ['like', "%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page') : 1;
        $limits = 10;// 获取总条数
        $count = Db::name('download')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $download= new DownloadModel();
        $lists = $download->getDownloadByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        if (input('get.page')) {
            return json($lists);
        }
        return $this->fetch();
    }


    /**
     * [add_article 添加文章]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function add_download()
    {
        if (request()->isAjax()) {

            $param = input('post.');
            $download = new DownloadModel();
            $flag = $download->insertDownload($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        return $this->fetch();
    }


    /**
     * [edit_article 编辑文章]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function edit_download()
    {
        $download = new DownloadModel();

        if (request()->isAjax()) {

            $param = input('post.');
            $flag = $download->updateDownload($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        $this->assign('download', $download->getOneDownload($id));
        return $this->fetch();
    }


    /**
     * [del_article 删除文章]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function del_download()
    {
        $id = input('param.id');
        $cate = new DownloadModel();
        $flag = $cate->delDownload($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }


    /**
     * [article_state 文章状态]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function download_state()
    {
        $id = input('param.id');
        $status = Db::name('download')->where(array('id' => $id))->value('state');//判断当前状态情况
        if ($status == 1) {
            $flag = Db::name('download')->where(array('id' => $id))->setField(['state' => 0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        } else {
            $flag = Db::name('download')->where(array('id' => $id))->setField(['state' => 1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }
}